import Utils from "../../../Utils";
import BlackHoleBullet from "../../flyer/DestructibleFlyer/FlyerMissile/BlackHoleBullet";
import { Killer } from "../../funcObj/interface/HealthPoint";
import { Location } from "../../info/Predefine";
import GameManager from "../../manager/GameManager";
import NodePoolManager from "../../manager/NodePoolManager";
import Player from "../../player/Player";
import LaunchableFalicity from "../LaunchableFalicity";

const { ccclass, property } = cc._decorator;

/**
 * 黑洞掠夺者
 */
@ccclass
export default class BlackHolePredator extends LaunchableFalicity {

    @property(cc.Node)
    creatProgressBar: cc.Node = null;

    @property(cc.Node)
    holdProgressBar: cc.Node = null;

    @property(cc.Node)
    points: cc.Node = null;

    blackHoleQueue: Array<cc.Node>;

    creating: boolean;

    get count(): number {
        return this.blackHoleQueue.length;
    }

    _creatProgress: number = 0;

    _holdProgress: number = 0;

    public set creatProgress(value: number) {
        let gress = Math.floor(value * 4);
        for (let i = 0; i < this.creatProgressBar.childrenCount; i++) {
            this.creatProgressBar.children[i].active = i < gress;
        }
        this._creatProgress = value
    }

    public get creatProgress(): number {
        return this._creatProgress
    }

    public set holdProgress(value: number) {
        let gress = Math.floor(value * 4);
        for (let i = 0; i < this.holdProgressBar.childrenCount; i++) {
            this.holdProgressBar.children[i].active = i < gress;
        }
        this._holdProgress = value
    }

    public get holdProgress(): number {
        return this._holdProgress
    }

    init(player: Player): void {
        super.init(player);
        this.creating = false;
        this.blackHoleQueue = [];
        this.creatProgress = 0;
        this.holdProgress = 0;
        this.creat();
    }

    creat() {
        if (this.creating) return;
        this.creatProgress = 0;
        this.holdProgress = 0;
        this.creating = true;
        cc.tween<BlackHolePredator>(this)
            .to(this.makingTime, { creatProgress: 1 })
            .call(() => {
                let blackHole = NodePoolManager.instance.obtain(NodePoolManager.instance.blackHole);
                blackHole.getComponent(BlackHoleBullet).init(this);
                let flyerParent = this.player.flyerParentNode;
                blackHole.parent = flyerParent;
                blackHole.position = flyerParent.convertToNodeSpaceAR(this.points.convertToWorldSpaceAR(cc.Vec3.ZERO));

                this.blackHoleQueue.push(blackHole);
                this.player.node.emit(Player.EventType.更新飞行物数量, this.facilityType, 1);
                this.creating = false;

                cc.tween<BlackHolePredator>(this)
                    .to(this.facilityInfo.蓄力所需时间, { holdProgress: 1 })
                    .start();
            })
            .start();
    }

    dispatch(target: Location) {
        super.dispatch(target);
        cc.Tween.stopAllByTarget(this);
        let blackHole: cc.Node = this.blackHoleQueue.shift();
        let blackHoleComp = blackHole.getComponent(BlackHoleBullet);
        blackHoleComp.setProgress(this.holdProgress);
        this.dispatchQueue.push({ flyer: blackHole, target: target })
        this.player.node.emit(Player.EventType.更新飞行物数量, this.facilityType, -1);
        this.creat();
    }

    die(killer: Killer): void {
        super.die(killer);
        this.blackHoleQueue.forEach(blackHole => NodePoolManager.instance.recycle(blackHole));
        this.blackHoleQueue = null;
        cc.Tween.stopAllByTarget(this);
    }
}
